package com.linsen.uaa.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.linsen.core.common.constant.Oauth2Constant;
import com.linsen.core.common.exception.RequestBusinessException;
import com.linsen.core.redis.util.RedisUtil;
import com.linsen.uaa.provider.bo.ImgCaptchaBO;
import com.linsen.uaa.service.IValidateService;
import com.wf.captcha.ArithmeticCaptcha;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.Duration;

/**
 * 验证码业务类
 *
 * @author zhaoyong
 * @date 2023/3/16
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ValidateServiceImpl implements IValidateService {
    private final RedisUtil<String> redisUtil;

    @Override
    public ImgCaptchaBO getImgCaptcha() {
        ImgCaptchaBO captcha = new ImgCaptchaBO();
        String uuid = IdUtil.simpleUUID();
        /// SpecCaptcha captcha = new SpecCaptcha(120, 40);
        /// String text = captcha.text();// 获取运算的结果：5
        ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(120, 40);
        arithmeticCaptcha.getArithmeticString();  // 获取运算的公式：3+2=?
        // 获取运算的结果：5
        String text = arithmeticCaptcha.text();
        this.redisUtil.getOpsForValue().set(Oauth2Constant.CAPTCHA_KEY + uuid, text, Duration.ofMinutes(30));
        captcha.setKey(uuid);
        captcha.setCodeUrl(arithmeticCaptcha.toBase64());
        return captcha;
    }

    @Override
    public void checkImgCaptcha(String key, String code) {
        String cacheCaptchaCode = this.redisUtil.getOpsForValue().get(Oauth2Constant.CAPTCHA_KEY + key);

        if (StrUtil.isBlank(code)) {
            throw new RequestBusinessException("请输入验证码");
        }
        if (ObjUtil.isNull(cacheCaptchaCode)) {
            throw new RequestBusinessException("验证码已过期");
        }
        if (!StrUtil.equalsIgnoreCase(code, cacheCaptchaCode)) {
            throw new RequestBusinessException("验证码不正确");
        }

        this.redisUtil.getRedisTemplate().delete(Oauth2Constant.CAPTCHA_KEY + key);
    }

    @Override
    public void sendSmsCaptcha(String mobile) {
        //TODO 短信实际发送逻辑待完善
        String code = "0291";
        this.redisUtil.getOpsForValue().set(Oauth2Constant.SMS_CODE_KEY + mobile, code, Duration.ofMinutes(5));
    }

    @Override
    public void checkSmsCaptcha(String mobile, String code) {
        String cacheCaptchaCode = this.redisUtil.getOpsForValue().get(Oauth2Constant.SMS_CODE_KEY + mobile);

        if (StrUtil.isBlank(code)) {
            throw new RequestBusinessException("请输入验证码");
        }
        if (ObjUtil.isNull(cacheCaptchaCode)) {
            throw new RequestBusinessException("验证码已过期");
        }
        if (!StrUtil.equalsIgnoreCase(code, cacheCaptchaCode)) {
            throw new RequestBusinessException("验证码不正确");
        }

        this.redisUtil.getRedisTemplate().delete(Oauth2Constant.SMS_CODE_KEY + mobile);
    }

}
